package com.itany.mall.jdbc.transaction.impl;

import java.sql.SQLException;

import com.itany.mall.factory.Component;
import com.itany.mall.jdbc.exception.DataAccessException;
import com.itany.mall.jdbc.transaction.TransactionManager;
import com.itany.mall.jdbc.util.JDBCUtil;

@Component
public class TransactionManagerImpl implements TransactionManager {

	@Override
	public void beginTransaction() {
		try {
			JDBCUtil.getConnection().setAutoCommit(false);
		} catch (SQLException e) {
			throw new DataAccessException("事务开启失败", e);
		}
	}

	@Override
	public void commit() {
		try {
			JDBCUtil.getConnection().commit();
			JDBCUtil.close(null, null, JDBCUtil.getConnection());
		} catch (SQLException e) {
			throw new DataAccessException("事务提交失败", e);
		}
	}

	@Override
	public void rollback() {
		try {
			JDBCUtil.getConnection().rollback();
			JDBCUtil.close(null, null, JDBCUtil.getConnection());
		} catch (SQLException e) {
			throw new DataAccessException("事务提交失败", e);
		}
	}

}
